<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem
          v-if="goods"
          :to="`/category/${goods.categories[1].id}`"
          >{{ goods.categories[1].name }}</XtxBreadItem
        >
        <XtxBreadItem
          v-if="goods"
          :to="`/category/sub/${goods.categories[0].id}`"
          >{{ goods.categories[0].name }}</XtxBreadItem
        >
        <XtxBreadItem to="/">华为</XtxBreadItem>
        <XtxBreadItem to="/">p30</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
        </div>
        <div class="spec">
          <!-- 商品名称 -->
          <GoodsName :goods="goods" />
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku" :skuId="`300276314`" />
          <!-- 数量组件 -->
          <XtxNumbox label="数量" v-model="num" />
          <!-- 按鸟组件 -->
          <XtxButton
            @click="insertCart"
            type="primary"
            style="margin-top: 20px"
            >加入购物车</XtxButton
          >
        </div>
      </div>

      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTans></GoodsTans>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarm />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot />
          <GoodsHot :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch, nextTick, provide } from "vue";
import { useRoute } from "vue-router";
import { findProduct } from "@/api/product";
import GoodsRelevant from "./components/goods-relevant.vue";
import GoodsImage from "./components/goods-image.vue";
import GoodsName from "./components/goods-name.vue";
import GoodsSku from "./components/goods-sku.vue";
import GoodsTans from "./components/goods-tabs.vue";
import GoodsHot from "./components/goods-hot.vue";
import GoodsWarm from "./components/goods-warn.vue";
import Message from "@/components/library/Message";
import { useStore } from 'vuex';

// test gooods url
// http://localhost:8080/product/3987562

export default {
  name: "XtxGoodsPage",
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsName,
    GoodsSku,
    GoodsTans,
    GoodsHot,
    GoodsWarm,
  },
  setup(props) {
    // 获取商品数据
    const store = useStore()
    const goods = getGoods();
    const num = ref(1); // 选择的数量

    const changeSku = (sku) => {
      // 修改商品的现价原价库存信息
      if (sku.skuId) {
        goods.value.price = sku.price;
        goods.value.oldPrice = sku.oldPrice;
        goods.value.inventory = sku.inventory;
      }

      // 记录选择后的sku，可能有数据，可能没有数据{}
      currSku.value = sku;
    };

    // 给所有后代组件传递goods 数据  provide传·ref不需要 .value  但子组件就需要 .value 获取值
    provide("goods", goods);

    // 记录选择的sku信息  选择不完整为null
    const currSku = ref(null);
    // 加入购物车
    const insertCart = () => {
      if (currSku.value && currSku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currSku.value;
        const { id, name, price, mainPictures } = goods.value;
        store.dispatch("cart/insertCart", {
            skuId,
            attrsText,
            stock,
            id,
            name,
            price,
            nowPrice: price,
            picture: mainPictures[0],
            selected: true,
            isEffective: true,
            count: num.value,
          })
          .then(() => {
            Message({ type: "success", text: "加入购物车成功" });
          });
      } else {
        Message({ text: "青选择完整规格" });
      }
    };

    return { goods, changeSku, num, insertCart };
  },
};

const getGoods = () => {
  const goods = ref(null);
  const route = useRoute();
  // 可能出现路由的params发生改变  页面不刷新
  watch(
    () => route.params.id,
    async (newValue) => {
      if (newValue && `/product/${newValue}` === route.path) {
        const { result } = await findProduct(route.params.id);
        goods.value = null;
        nextTick(() => {
          goods.value = result;
        });
      }
    },
    { immediate: true }
  );
  return goods;
};
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>